# coding:utf-8
"""
add by chenhj at 2017-10-30
给暂时还没有用户协议的用户插入用户协议信息
"""

import os
import set_env_path     # 请勿删除


from sqlalchemy import create_engine
from sqlalchemy.sql import text


engine = create_engine(
    os.getenv('DATABASE_URL')
    or 'mysql+pymysql://root:@127.0.0.1/wph_www_prod?charset=utf8'
)
conn = engine.connect()


def insert_user_protocol():
    """
    created by chenhj at 2017/10/30

    """
    insert_protocol_sql = text("""
        insert into user_protocol(user_id, mobile, nickname, version, created_at, updated_at)
        values (:user_id, :mobile, :nickname, '1.0', :created_at, :updated_at)
    """)

    select_no_protocol_user_sql = text("""
        select user.id, user.created_at, user.mobile, user.nickname from user
        where user.id not in (select user_id from user_protocol)
         and user.type = 100
    """)
    select_result = conn.execute(select_no_protocol_user_sql)
    for each in select_result:
        conn.execute(
            insert_protocol_sql,
            {
                'user_id': each.id,
                'mobile': each.mobile,
                'nickname': each.nickname,
                'created_at': each.created_at,
                'updated_at': each.created_at
            }
        )


if __name__ == '__main__':
    insert_user_protocol()
